//
//  NetworkRequest+LicaiFuwu.m
//  URLDemo
//
//  Created by rimi on 15/10/9.
//  Copyright (c) 2015年 yangqin. All rights reserved.
//

#import "NetworkRequest+LicaiFuwu.h"
#import "Parse+parseAll.h"

@implementation NetworkRequest (LicaiFuwu)
- (void)getLicaiFuwuWithParameters:(ZLTypeCode)parameters successHandle:(LicaiSuccessBlock)success failureHandle:(failureBlock)faile {
    NSDictionary *parametersDic = @{@"pageSize": @(10),
                                    @"startNum": @0,
                                    @"typeId" : @"",
                                    @"typeCode": @(parameters)
                                    };
    __weak typeof(self)weakSelf = self;
    [self post:PRODUCT_LIST_URL parameters:parametersDic successHandle:^(NSString *responds) {
        if (success) {
            NSArray *rtnArray = [weakSelf parseLicaiFuwu:responds];
            success(rtnArray);
        };
    } failureHandle:^(NSError *error) {
        
    }];
}

- (void)searchLicaiFuwuWithTypeCode:(ZLTypeCode)parameters proName:(NSString *)inputName successHandle:(LicaiSuccessBlock)success failureHandle:(failureBlock)faile {
    NSDictionary *parameterDic = @{@"searcherType": @0,
                                   @"proName":inputName,
                                   @"startNum":@0,
                                   @"pageSize":@10,
                                   @"typeCode": @(parameters),
                                  };
    __weak typeof(self)weakSelf = self;
    [self post:PRODUCT_INFO_NAME_URL parameters:parameterDic successHandle:^(NSString *responds) {
        if (success) {
            success([weakSelf parseSearch:responds]);
        }
    } failureHandle:^(NSError *error) {
        
    }];
}

- (void)searchLicaiFuwuWithTypeCode:(ZLTypeCode)parameters productCode:(NSString *)praductCode successHandle:(DetailServiceBlock)success fialureHandle:(failureBlock)failure {
    NSDictionary *parameterDic = @{
                                   @"productCode":praductCode,
                                   @"typeCode": @(parameters),
                                   };
    __weak typeof(self)weakSelf = self;
    [self post:PRODUCT_INFO_ID_URL parameters:parameterDic successHandle:^(NSString *responds) {
        if (success) {
            NSDictionary *orgDic = [weakSelf parseAllNode:responds nodePath:@"////product"];
            NSDictionary *rtnDic = [weakSelf covertUrlWithUrlkey:@"showImgUrl" orgDic:orgDic];
            success(rtnDic);
        }
    } failureHandle:^(NSError *error) {
        
    }];
}

// 解析理财服务列表
- (NSArray *) parseLicaiFuwu:(NSString *)parseStr {
    NSArray *nodeNames = @[@"productName", @"custodian",@"nav", @"totalNav", @"founType", @"showImgUrl", @"prouductCode", @"riskLevel", @"isHot", @"isNew"];
    Parse *parse = [[Parse alloc] init];
    NSArray *resultArr = [parse parse:parseStr nodePath:@"////com.chinaxyl.product.pojo.FoundationPojo" nodeNames:nodeNames];
    return resultArr;
}

// 解析搜索
- (NSArray *) parseSearch :(NSString *)parseStr {
    NSArray *nodeNames = @[@"name", @"typeCode", @"productCode"];
    Parse *parse = [[Parse alloc] init];
    NSArray *resultArr = [parse parse:parseStr nodePath:@"////product" nodeNames:nodeNames];
    return resultArr;
}

// 解析所有
- (NSDictionary *)parseAllNode:(NSString *)parseStr nodePath: (NSString *) nodePath {
    Parse *parse = [[Parse alloc] init];
    NSDictionary *resultDic = [parse parseAllChildren:parseStr nodePath:nodePath].firstObject;
    return resultDic;
}
@end
